package com.ffcs.cn.action.desktopcontrol.smToolbarMenu;

import com.supermap.desktop.controls.ui.controls.SmDialog;
import com.supermap.desktop.controls.ui.controls.toolBarJmenu.SmToolbarMenu;
import com.supermap.desktop.controls.utilities.ComponentFactory;
import com.supermap.desktop.controls.utilities.ToolbarIconUtilities;
import com.supermap.desktop.core.ui.controls.GridBagConstraintsHelper;
import com.supermap.desktop.core.utilties.TableUtilities;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import javax.swing.table.AbstractTableModel;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.util.ArrayList;
import java.util.List;


public class DialogSmToolbarMenu extends SmDialog {

	private JLabel countLabel;
	private JLabel textCountLabel;

	private JCheckBox enableCheckbox;

	private JLabel showFunctionTextLabel;
	private JTextField showFunctionTextField;

	private JLabel showFunctionTipLabel;
	private JTextField showFunctionTipTextField;

	private JLabel arrowTipLabel;
	private JTextField arrowTipJTextField;

	private JTable table;
	private TableModelMenuItem tableModelMenuItem;

	private JToolBar toolBar;
	private JButton addButton;
	private JButton deleteButton;

	private SmToolbarMenu smToolbarMenu;

	public DialogSmToolbarMenu() {
		setTitle("SmToolbarMenu 示例");
		setSize(new Dimension(460, 430));
		initComponents();
		initLayout();
		initListener();
	}

	private void initComponents() {
		initLeftPanel();
		initRightPanel();
	}

	private void initLeftPanel(){
		smToolbarMenu = new SmToolbarMenu(ToolbarIconUtilities.getIconByPath(ToolbarIconUtilities.ADD_24));
		smToolbarMenu.addJMenuItem(new JMenuItem("选项1"));
		smToolbarMenu.addJMenuItem(new JMenuItem("选项2"));
		smToolbarMenu.setShowFunctionText("文本");
		smToolbarMenu.setShowFunctionTip("提示信息");
		smToolbarMenu.setArrowTip("下拉按钮提示信息");
	}

	private void initRightPanel() {

		countLabel = new JLabel("下拉项个数：");
		textCountLabel = new JLabel(smToolbarMenu.getJMenuItemCount() + " 个");

		enableCheckbox = new JCheckBox("可用");
		enableCheckbox.setSelected(smToolbarMenu.isEnabled());

		showFunctionTextLabel = new JLabel("按钮文本：");
		showFunctionTextField = new JTextField("文本");
		showFunctionTextField.setToolTipText("失去焦点自动应用");

		showFunctionTipLabel = new JLabel("按钮提示：");
		showFunctionTipTextField = new JTextField("提示信息");
		showFunctionTipTextField.setToolTipText("失去焦点自动应用");

		arrowTipLabel = new JLabel("下拉按钮提示：");
		arrowTipJTextField = new JTextField("下拉按钮提示信息");
		arrowTipJTextField.setToolTipText("失去焦点自动应用");

		tableModelMenuItem = new TableModelMenuItem();
		table = new JTable(tableModelMenuItem);


		toolBar = new JToolBar();
		toolBar.setFloatable(false);
		addButton = ComponentFactory.createToolBarButtonAddData();
		deleteButton = ComponentFactory.createToolBarButtonDelete();
		deleteButton.setEnabled(false);
		toolBar.add(addButton);
		toolBar.add(deleteButton);

	}

	//获取所有下拉项的文本内容
	private List<String> getSmToolbarMenuItems() {
		int count = smToolbarMenu.getJMenuItemCount();
		ArrayList<String> list = new ArrayList<>(count);
		for (int i = 0; i < count; i++) {
			list.add(smToolbarMenu.getJMenuItem(i).getText());
		}
		return list;
	}

	private void initLayout() {
		this.setLayout(new GridBagLayout());
		this.add(getLeftPanel(), new GridBagConstraintsHelper(0, 0, 1, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.CONTROLS_GAP)
				.setWeight(1, 1)
				.setFill(GridBagConstraints.BOTH));
		this.add(getRightPanel(), new GridBagConstraintsHelper(1, 0, 1, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, 0, GridBagConstraintsHelper.FRAME_CONTROL_GAP,  GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setWeight(1, 1)
				.setFill(GridBagConstraints.BOTH));
	}

	private JPanel getLeftPanel(){
		JPanel panel = new JPanel();
		panel.add(smToolbarMenu);
		panel.setBorder(new TitledBorder("控件"));
		return panel;
	}

	private JPanel getRightPanel() {
		JPanel panel = new JPanel();
		panel.setLayout(new GridBagLayout());

		panel.add(countLabel, new GridBagConstraintsHelper(0, 0, 1, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, 0, 0)
				.setAnchor(GridBagConstraints.WEST));
		panel.add(textCountLabel, new GridBagConstraintsHelper(1, 0, 1, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.CONTROLS_GAP, 0, GridBagConstraintsHelper.CONTROLS_GAP)
				.setAnchor(GridBagConstraints.WEST));

		panel.add(enableCheckbox, new GridBagConstraintsHelper(0, 1, 2, 1)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, 0, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setAnchor(GridBagConstraints.WEST));

		panel.add(showFunctionTextLabel, new GridBagConstraintsHelper(0, 2, 1, 1)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, 0, 0)
				.setAnchor(GridBagConstraints.WEST));
		panel.add(showFunctionTextField, new GridBagConstraintsHelper(1, 2, 1, 1)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.CONTROLS_GAP, 0, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setWeight(1, 0)
				.setFill(GridBagConstraints.HORIZONTAL)
				.setAnchor(GridBagConstraints.WEST));

		panel.add(showFunctionTipLabel, new GridBagConstraintsHelper(0, 3, 1, 1)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, 0, 0)
				.setAnchor(GridBagConstraints.WEST));
		panel.add(showFunctionTipTextField, new GridBagConstraintsHelper(1, 3, 1, 1)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.CONTROLS_GAP, 0, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setWeight(1, 0)
				.setFill(GridBagConstraints.HORIZONTAL)
				.setAnchor(GridBagConstraints.WEST));

		panel.add(arrowTipLabel, new GridBagConstraintsHelper(0, 4, 1, 1)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, 0, 0)
				.setAnchor(GridBagConstraints.WEST));
		panel.add(arrowTipJTextField, new GridBagConstraintsHelper(1, 4, 1, 1)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.CONTROLS_GAP, 0, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setWeight(1, 0)
				.setFill(GridBagConstraints.HORIZONTAL)
				.setAnchor(GridBagConstraints.WEST));

		panel.add(toolBar,new GridBagConstraintsHelper(0, 5, 2, 1)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, 0, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setAnchor(GridBagConstraints.WEST));

		JScrollPane scrollPane = new JScrollPane(table);
		panel.add(scrollPane,new GridBagConstraintsHelper(0, 6, 2, 1)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, 0, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setWeight(1,1)
				.setFill(GridBagConstraints.BOTH)
				.setAnchor(GridBagConstraints.WEST));

		panel.setBorder(new TitledBorder("控制面板"));
		return panel;
	}

	private void initListener() {
		//创建点击事件
		ActionListener actionListener = e -> {
			if (e.getSource() == addButton){
				add();
			}else if(e.getSource() == deleteButton){
				delete();
			}else if (e.getSource() == enableCheckbox){
				smToolbarMenu.setEnabled(enableCheckbox.isSelected());
			}
		};

		//添加点击事件监听
		addButton.addActionListener(actionListener);
		deleteButton.addActionListener(actionListener);
		enableCheckbox.addActionListener(actionListener);

		//创建焦点事件
		FocusAdapter focusAdapter = new FocusAdapter() {
			//焦点失去
			@Override
			public void focusLost(FocusEvent e) {
				if (e.getSource() == showFunctionTextField){
					smToolbarMenu.setShowFunctionText(showFunctionTextField.getText());
				}else if (e.getSource() == showFunctionTipTextField){
					smToolbarMenu.setShowFunctionTip(showFunctionTipTextField.getText());
				}else if (e.getSource() == arrowTipJTextField){
					smToolbarMenu.setArrowTip(arrowTipJTextField.getText());
				}
			}
		};
		//添加焦点事件监听
		showFunctionTextField.addFocusListener(focusAdapter);
		showFunctionTipTextField.addFocusListener(focusAdapter);
		arrowTipJTextField.addFocusListener(focusAdapter);

		//添加 table 选择事件监听
		table.getSelectionModel().addListSelectionListener(e -> {
			int[] rows = table.getSelectedRows();
			deleteButton.setEnabled(rows != null && rows.length >0);
		});

	}

	//添加下拉项
	private void add(){
		addMenuItem();
		int rowCount = tableModelMenuItem.getRowCount();
		//更新表格数据
		tableModelMenuItem.updateTableData();
		//更新个数
		updateCountText();
		//在表格中选中新添加的下拉项
		TableUtilities.setTableSelectedRows(table,rowCount);
		//滚动条定位到新添加的位置
		TableUtilities.scrollToVisible(table,rowCount,rowCount);
	}

	//删除下拉项
	private void delete(){
		int[] selectedRows = table.getSelectedRows();
		if (selectedRows != null && selectedRows.length >0) {
			for (int i = selectedRows.length - 1; i >= 0; i--) {
				smToolbarMenu.removeMenuItem(selectedRows[i]);
			}
			tableModelMenuItem.updateTableData();
			updateCountText();
			TableUtilities.setSelectedRowByDeleted(table,selectedRows);
		}
	}

	private void updateCountText(){
		textCountLabel.setText(smToolbarMenu.getJMenuItemCount()+" 个");
	}

	//为 smToolbarMenu 添加下拉项
	private void addMenuItem(){
		int itemCount = smToolbarMenu.getJMenuItemCount();
		JMenuItem menuItem = new JMenuItem("选项" + itemCount);
		smToolbarMenu.addJMenuItem(menuItem);
	}


	private class TableModelMenuItem extends AbstractTableModel{
		private String[] columns = {"序号","下拉项"};
		private List<String> items;

		private static final int INDEX = 0;
		private static final int ITEM = 1;

		TableModelMenuItem() {
			items = getSmToolbarMenuItems();
		}

		@Override
		public int getRowCount() {
			return items.size();
		}

		@Override
		public int getColumnCount() {
			return columns.length;
		}

		@Override
		public Object getValueAt(int rowIndex, int columnIndex) {
			switch (columnIndex){
				case INDEX:
					return rowIndex;
				case ITEM:
					return items.get(rowIndex);
			}
			return null;
		}

		@Override
		public String getColumnName(int column) {
			return columns[column];
		}

		//更新数据
		void updateTableData(){
			items = getSmToolbarMenuItems();
			//刷新表格数据
			fireTableDataChanged();
		}
	}
}
